﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using jSLCore.Client.Presentation.AuthorizeService;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLCore.Client.Presentation.Authorization.Managment
{
    public partial class RolesList : SelectControl
    {
        public RolesList()
        {
            InitializeComponent();
        }

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }
        
        protected override void AcceptButtonClicked()
        {
            Search();
        }

        private void Search()
        {
            DoWork("Загрузка ролей",(sender, args) =>
                                        {
                                            args.Result = Communication.Proxy.SearchRoles(args.String);
                                        },(o, eventArgs) =>
                                                              {
                                                                  if(eventArgs.Error==null)
                                                                  {
                                                                      grid.ItemsSource = (IEnumerable) eventArgs.Result;
                                                                  }
                                                              },txt.Text);
        }

        public override void LoadInitialData()
        {
            Search();
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        public override bool IsStandartOkCancelEnabledWhenNotSelectable
        {
            get { return true; }
        }

        

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        private void gotoRole_CLicked(object sender, RowSelectedEventArgs e)
        {
            var role = (Role)e.RowObject;

            new RoleDetail(role.id).ShowDialog(this);
        }

        private void addRole_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new jAskForStringBox("Введите название роли",string.Empty,false);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   DoWork("Создание роли",(sender1, args) =>
                                                              {
                                                                  Communication.Proxy.CreateRole(args.String);
                                                              },(o1, eventArgs) =>
                                                                                     {
                                                                                         if(eventArgs.Error==null)
                                                                                         {
                                                                                             InfoBox.ShowDialog("Роль создана");
                                                                                             LoadInitialData();
                                                                                         }
                                                                                     },data);
                               },this );
        }

        private void delete_Clicked(object sender, RowSelectedEventArgs rowSelectedEventArgs)
        {
            /*var element = (FrameworkElement)sender;
            var role = (Role)element.DataContext;*/
            var role = (Role)rowSelectedEventArgs.RowObject;
            var dlg = new QuestionBox("Вы действительно хотите удалить роль?",QuestionBoxType.OkCancel);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   DoWork("Удаление роли", (sender1, args) =>
                                                               {
                                                                   Communication.Proxy.DeleteRole(args.Guid);
                                                               }, (o1, eventArgs) =>
                                                                                       {
                                                                                           if(eventArgs.Error==null)
                                                                                           {
                                                                                               InfoBox.ShowDialog("Роль удалена");
                                                                                               LoadInitialData();
                                                                                           }
                                                                                       }, role.id);
                               },this );
        }
    }
}
